package com.itzhoun;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 周楠
 * @Date 2023/01/28/下午 12:24
 */
public class WeChatServer implements Subject{

    private List<Observer> observers;
    private String message;

    public WeChatServer() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);


    }

    @Override
    public void removeObserver(Observer observer) {
        if (!observers.isEmpty()){
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update(message);
        }

    }

    public void inform(String message){
        this.message = message;
        System.out.println("微信服务更新：" + message);
        //消息更新通知所有观察者
        notifyObserver();

    }
}
